#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                     
#   saggau@gmail.com                                                       
#                                                                          
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                    
#                                                                          
#   This program is distributed in the hope that it will be useful,        
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                          
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                        
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#

maxoverlaps = 36
overlaps = [x + 1 for x in range(maxoverlaps)]

generic = """
SynthDef("NAME", 
{ ARGS
BODY
}).writeDefFile;
"""

args = {1 : {"outBus":0, "bufnum":0, "in":1, "thresh": 0.004, "amp": 1,
                  "duration":0.2, "triggerID":1, "envCurve":-7,
                  "attackDuration":0.05, "pan":0, "bufferPosition":200},
        2 : {"outBus":0, "bufnum":0, "in":1, "thresh": 0.004, "amp": 1,
                    "duration":0.2, "triggerID":1, "envCurve":-7,
                    "attackDuration":0.05, "bufferPosition":200}
        }

outlines = {1 : "Out.ar(outBus, Pan2.ar(sum(outList), pan))",
            2 : "Out.ar(outBus, sum(outList))"}

synths = {"static" : "1",
          "dynamic" : "PulseCount.kr(trig)"}

envs = {"percussive" : """Env.perc(attackDuration,
                    (duration - attackDuration), outamp, envCurve)"""}
                    
synthTypes = synths.keys()
envTypes = envs.keys()

body = \
"""
var trig, inAmp, gatedAmp, triggerBool, outamp, bufferFrames, bufRateScale;
var bufPointer, env, envGenList, outList, trigList, maxOverlaps;

maxOverlaps = NUMBER_OF_OVERLAPS;

bufferFrames = BufFrames.kr(bufnum);
bufRateScale = BufRateScale.kr(bufnum);
inAmp = Amplitude.kr (AudioIn.ar(in));
triggerBool = (AudioIn.ar(in) >= thresh);
gatedAmp = Latch.kr(inAmp, triggerBool);
outamp = (gatedAmp * amp);

trig = Trig.kr(triggerBool, duration / maxOverlaps);
SendTrig.kr (trig, triggerID, outamp); //send a trigger to the client

bufPointer = BUFPOINTER_LINE;
//Buffer position pointer.  
//This gets moved after each trigger in the dynamic version

env = ENV_LINE;
//an envelope (Env.something)

trigList = List.series(maxOverlaps);
envGenList = List.series(maxOverlaps);
outList = List.series(maxOverlaps);


maxOverlaps.do({ arg i;
	trigList.put(i, PulseDivider.kr(trig, maxOverlaps, i));
	envGenList.put(i, EnvGen.kr(env, trigList.at(i)));
	outList.put(i, PlayBuf.ar(1, bufnum, bufRateScale, trigList.at(i),
	            (bufPointer*bufferPosition)%bufferFrames)*envGenList.at(i));
   });
OUT_LINE;
"""